﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LCA.Community.Web.Models;
using Microsoft.Practices.Unity;
using LCA.Community.DAL.Interfaces;
using LCA.Community.Web.Filters;

namespace LCA.Community.Web.Controllers
{
    /// <summary>
    /// Class for managing search actions.
    /// </summary>
    public class SearchController : Controller
    {
        private SearchEngine searchEngine;
        private int elementsPerPage = 5;

        /// <summary>
        /// Initializes a new instance of the <see cref="SearchController"/> class.
        /// </summary>
        public SearchController()
        {
            
        }

        /// <summary>
        /// Action for searching questions on the system.
        /// </summary>
        /// <param name="topic">The topic.</param>
        /// <param name="page">The page.</param>
        /// <returns>
        /// View showing the result of the search.
        /// </returns>
        [ErrorFilter(typeof(Exception), "RequestError", 1)]
        public ActionResult SearchQuestions(string topic, int page)
        {
            var questions = QuestionsRepository.Entities;
            searchEngine = new SearchEngine(Url);
            var searchResult = searchEngine.SearchQuestions(questions, topic, elementsPerPage, page);

            ViewData["currentpage"] = page;
            ViewData["pages"] = Convert.ToInt32(Math.Ceiling((double)(QuestionsRepository.Entities.Count / (double)elementsPerPage)));
            ViewData["elementsPerPage"] = elementsPerPage;

            ViewData["type"] = "questions";
            ViewData["topic"] = topic;
            ViewData["searchquery"] = "Resultados de busqueda de preguntas";

            return View("SearchResults",searchResult);
        }

        /// <summary>
        /// Action for searching users on the system.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="city">The city.</param>
        /// <param name="country">The country.</param>
        /// <param name="email">The email.</param>
        /// <param name="interests">The interests.</param>
        /// <param name="age">The age.</param>
        /// <param name="page">The page.</param>
        /// <returns>View showing the result of the search.</returns>
        [ErrorFilter(typeof(Exception), "RequestError", 1)]
        public ActionResult SearchUser(string name, string city, string country, string email, string interests, int? age, int page)
        {
            var users = UsersRepository.Entities;
            searchEngine = new SearchEngine(Url);
            var searchResult = searchEngine.SearchUsers(users, name, city, country, interests, age, elementsPerPage, page);

            ViewData["currentpage"] = page;
            ViewData["pages"] = Convert.ToInt32(Math.Ceiling((double)(UsersRepository.NonAnonymousUsers.Count / (double)elementsPerPage)));
            ViewData["elementsPerPage"] = elementsPerPage;

            ViewData["searchquery"] = "Resultados de busqueda de usuarios";
            ViewData["currentpage"] = page;
            ViewData["type"] = "user";
            ViewData["name"] = name;
            ViewData["city"] = city;
            ViewData["country"] = country;
            ViewData["email"] = email;
            ViewData["interests"] = interests;
            ViewData["age"] = age;

            return View("SearchResults", searchResult);
        }

        /// <summary>
        /// Action for searching groups on the system.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="owner">The owner.</param>
        /// <param name="site">The site.</param>
        /// <param name="page">The page.</param>
        /// <returns>View showing the result of the search.</returns>
        [ErrorFilter(typeof(Exception), "RequestError", 1)]
        public ActionResult SearchGroup(string name, string owner, string site, int page)
        {
            var groups = GroupsRepository.Entities;
            searchEngine = new SearchEngine(Url);
            var searchResult = searchEngine.SearchGroups(groups,name,owner,site,elementsPerPage,page);

            ViewData["currentpage"] = page;
            ViewData["pages"] = Convert.ToInt32(Math.Ceiling((double)(GroupsRepository.Entities.Count / (double)elementsPerPage)));
            ViewData["elementsPerPage"] = elementsPerPage;

            ViewData["searchquery"] = "Resultados de busqueda de grupos";
            ViewData["type"] = "group";
            ViewData["name"] = name;
            ViewData["owner"] = owner;
            ViewData["site"] = site;

            return View("SearchResults", searchResult);
        }

        /// <summary>
        /// Action for searching articles on the system.
        /// </summary>
        /// <param name="title">The title.</param>
        /// <param name="author">The author.</param>
        /// <param name="tags">The tags.</param>
        /// <param name="page">The page.</param>
        /// <returns>View showing the result of the search.</returns>
        [ErrorFilter(typeof(Exception), "RequestError", 1)]
        public ActionResult SearchArticle(string title, string author, string tags, int page)
        {
            var posts = PostRepository.Entities;
            searchEngine = new SearchEngine(Url);
            var searchResult = searchEngine.SearchArticles(posts, title, author, tags, elementsPerPage, page);

            ViewData["currentpage"] = page;
            ViewData["pages"] = Convert.ToInt32(Math.Ceiling((double)(PostRepository.Entities.Count / (double)elementsPerPage)));
            ViewData["elementsPerPage"] = elementsPerPage;

            ViewData["type"] = "article";
            ViewData["searchquery"] = "Resultados de busqueda de artículos";
            ViewData["title"] = title;
            ViewData["author"] = author;
            ViewData["tags"] = tags;

            return View("SearchResults", searchResult);
        }

        [Dependency]
        public IQuestionsRepository QuestionsRepository { get; set; }
        [Dependency]
        public IUsersRepository UsersRepository { get; set; }
        [Dependency]
        public IGroupsRepository GroupsRepository { get; set; }
        [Dependency]
        public IPostsRepository PostRepository { get; set; }
    }
}
